import { createRouter, createWebHistory } from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/',
            redirect: 'login',
        },
        // 管理员登录
        {
            path: '/adminLogin',
            name: 'AdminLogin',
            component: () => import("../views/admin/Login.vue"),
        },
        // 管理员界面
        {
            path: '/adminMain',
            name: 'AdminMain',
            redirect: 'adminMain/feedbackManage',
            // meta: { requiresAuth: true,isAdminPage: true },
            component: () => import("../views/admin/Main.vue"),
            children: [
                {
                    path: 'feedbackManage',
                    name: 'FeedbackManage',
                    meta: { title: "反馈信息管理" },
                    component: () => import("../components/admin/FeedbackManage.vue")
                },
                {
                    path: 'recruiterManage',
                    name: 'RecruiterManage',
                    meta: { title: "招聘者信息管理" },
                    component: () => import("../components/admin/RecruiterManage.vue")
                },
                {
                    path: 'seekerManage',
                    name: 'SeekerManage',
                    meta: { title: "求职者信息管理" },
                    component: () => import("../components/admin/SeekerManage.vue")
                },
                {
                    path: 'jobManage',
                    name: 'JobManage',
                    meta: { title: "招聘岗位信息管理" },
                    component: () => import("../components/admin/JobManage.vue")
                },
                {
                    path: 'jobTypeManage',
                    name: 'JobTypeManage',
                    meta: { title: "岗位类型管理" },
                    component: () => import("../components/admin/JobTypeManage.vue")
                },
                {
                    path: 'skillManage',
                    name: 'SkillManage',
                    meta: { title: "技能类型管理" },
                    component: () => import("../components/admin/SkillManage.vue")
                }
            ]
        },
        // 招聘者 & 求职者登录
        {
            path: '/login',
            name: 'Login',
            component: () => import("../views/other/Login.vue"),
        },
        // 求职者界面
        {
            path: '/seekerMain',
            name: 'SeekerMain',
            redirect: 'seekerMain/seekerHome',
            meta: { requiresAuth: true, isSeekerPage: true },
            component: () => import("../views/other/job_seeker/SeekerMain.vue"),
            children: [
                {
                    path: 'seekerHome',
                    name: 'SeekerHome',
                    component: () => import("../views/other/job_seeker/SeekerHome.vue"),
                },
                {
                    path: 'seekerJobMessage/:id',
                    name: 'SeekerJobMessage',
                    component: () => import("../components/job_seeker/SeekerJobMessage.vue"),
                },
                {
                    path: 'searchMain',
                    name: 'SearchMain',
                    component: () => import("../components/job_seeker/SearchMain.vue"),
                },
                {
                    path: 'selfMessageSeeker',
                    name: 'SelfMessageSeeker',
                    component: () => import("../components/job_seeker/SelfMessageSeeker.vue"),
                },
            ]
        },
        // 招聘者界面
        {
            path: '/recruiterMain',
            name: 'RecruiterMain',
            redirect: 'recruiterMain/recruiterHome',
            meta: { requiresAuth: true,isRecruiterPage: true },
            component: () => import("../views/other/job_recruiter/RecruiterMain.vue"),
            children: [
                {
                    path: 'recruiterHome',
                    name: 'RecruiterHome',
                    meta: { title: "首页" },
                    component: () => import("../components/job_recruiter/RecruiterHome.vue")
                },
                {
                    path: 'recommendSeeker',
                    name: 'RecommendSeeker',
                    meta: { title: "推荐求职者" },
                    component: () => import("../components/job_recruiter/RecommendSeeker.vue")
                },
                {
                    path: 'postJob',
                    name: 'PostJob',
                    meta: { title: "发布岗位" },
                    component: () => import("../components/job_recruiter/PostJob.vue")
                },
                {
                    path: 'selfManage',
                    name: 'SelfManage',
                    meta: { title: "个人中心" },
                    redirect: 'selfManage/selfMessage',
                    component: () => import("../components/job_recruiter/SelfManage.vue"),
                    children: [
                        {
                            path: 'selfMessage',
                            name: 'SelfMessage',
                            meta: { title: "个人资料" },
                            component: () => import("../components/job_recruiter/SelfMessage.vue"),
                        },
                        {
                            path: 'jobMessage',
                            name: 'JobMessage',
                            meta: { title: "已发布招聘信息" },
                            component: () => import("../components/job_recruiter/JobMessage.vue"),
                        },
                        {
                            path: 'passwordChange',
                            name: 'PasswordChange',
                            meta: { title: "账号设置" },
                            component: () => import("../components/job_recruiter/PasswordChange.vue"),
                        },
                        {
                            path: 'jobMessageDetail',
                            name: 'JobMessageDetail',
                            meta: { title: "招聘详细信息" },
                            component: () => import("../components/job_recruiter/JobMessageDetail.vue"),
                        },
                    ]
                },
            ]
        }
    ]
})

// 路由前置守卫
router.beforeEach((to,from,next) => {
    NProgress.start(); // 开始进度条
    const isAuthenticated = !!localStorage.getItem('seeker');
    const isAuthenticated1 = !!localStorage.getItem('recruiter');
    const isAuthenticated2 = !!localStorage.getItem('admin');
    // 路由需要登陆但用户未认证
    if(to.meta.requiresAuth) {
        // 是招聘者 | 求职者页面
        if((to.meta.isRecruiterPage && !isAuthenticated1) || (to.meta.isSeekerPage && !isAuthenticated)) {
            next({ name: 'Login' });
        } else if(to.meta.isAdminPage && !isAuthenticated2) { // 是管理员页面
            next({ name: 'AdminLogin'});
        } else { // 已认证情况下放行
            next();
        }
    } else { // 不需要认证情况放行
        next();
    }
})

// 路由后置守卫
router.afterEach(() => {
    NProgress.done(); // 关闭进度条
});
export default router;
